package com.bgrconsulting.stockorderentry.service.rest.impl;

import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import com.bgrconsulting.stockorderentry.service.converter.PreviewChangeEquityOrderConverter;
import com.bgrconsulting.stockorderentry.service.converter.response.PreviewChangeEquityOrderResponseConverter;
import com.bgrconsulting.stockorderentry.service.dao.PreviewChangeEquityOrderResponseDao;
import com.bgrconsulting.stockorderentry.service.model.response.EquityOrderResponse;
import com.bgrconsulting.stockorderentry.service.model.response.PreviewChangeEquityOrderResponse;

@Path("/order/rest/previewchangeequityorder/")
public class PreviewChangeEquityOrderResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;
	/*public PreviewEquityOrderResource(UriInfo uriInfo, Request request, String id) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.id = id;
	}*/

	@POST
    @Consumes({"application/xml","application/json"})
    public Response createNewPreviewEquityOrder(JAXBElement<PreviewChangeEquityOrderConverter> previewEquityOrder) {
        
		
		PreviewChangeEquityOrderResponse previewEquityOrderResponse = new PreviewChangeEquityOrderResponse(new EquityOrderResponse());
		
				
		PreviewChangeEquityOrderResponseConverter peorc = new PreviewChangeEquityOrderResponseConverter(previewEquityOrderResponse);
		
		return putAndGetResponse(peorc);
		
    }
	
	private Response putAndGetResponse(PreviewChangeEquityOrderResponseConverter previewEquityOrderResponse) {
		Response res;
		if(PreviewChangeEquityOrderResponseDao.instance.getModel().containsKey(previewEquityOrderResponse.getEquityOrderResponse().getAccountId())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		PreviewChangeEquityOrderResponseDao.instance.getModel().put(previewEquityOrderResponse.getEquityOrderResponse().getAccountId(), previewEquityOrderResponse);
		return res;
	}
}

